package main

import "fmt"

/* 1342. 将数字变成 0 的操作次数，https://leetcode.cn/problems/number-of-steps-to-reduce-a-number-to-zero/ */

func ExecQ1342() {
	fmt.Println(numberOfSteps(14))
	fmt.Println(numberOfSteps(8))
	fmt.Println(numberOfSteps(123))
}

// 我写的
func numberOfSteps1(num int) int {
	count := 0
	for num > 0 {
		count++
		if num%2 == 1 {
			num = num - 1
		} else {
			num = num / 2
		}
	}
	return count
}

// 官方的
func numberOfSteps(num int) int {
	count := 0
	for ; num > 0; num >>= 1 {
		count += num & 1
		if num > 1 {
			count++
		}
	}
	return count
}
